#ifndef _PORT_INTERNAL_H
#define _PORT_INTERNAL_H

#include "port.h"

/* STM32系列芯片宏定义 - 根据实际使用的芯片选择对应宏 */
#ifndef STM32F4xx
  #define STM32F4xx  /* 默认使用STM32F4系列 */
#endif

/* 根据STM32系列自动包含对应的HAL库头文件 */
#if defined(STM32F1xx)
  #include "stm32f1xx_hal.h"
#elif defined(STM32F3xx)
  #include "stm32f3xx_hal.h"
#elif defined(STM32F4xx)
  #include "stm32f4xx_hal.h"
#elif defined(STM32G4xx) || defined(STM32G431xx)
  #include "stm32g4xx_hal.h"
/* Add other STM32 families as needed */
#else
  #error "No STM32 device family defined!"
#endif

/* 中断优先级配置 - 可在项目中重新定义 */
#ifndef MB_USART_IRQ_priority
  #define MB_USART_IRQ_priority     3     /* UART中断优先级 */
#endif
#ifndef MB_USART_IRQ_subpriority
  #define MB_USART_IRQ_subpriority  1     /* UART中断子优先级 */
#endif

#ifndef MB_TIM7_IRQ_priority
  #define MB_TIM7_IRQ_priority     4       /* 定时器中断优先级 */
#endif
#ifndef MB_TIM7_IRQ_subpriority
  #define MB_TIM7_IRQ_subpriority  1       /* 定时器中断子优先级 */
#endif

/* USART选择配置 - 可在项目中重新定义 */
#ifndef MB_USART_NR
  #define MB_USART_NR   2                 /* 默认使用USART2 */
#endif

/* RS485配置 - 可在项目中重新定义 */
#ifndef MB_USE_RS485
  #define MB_USE_RS485  1                 /* 是否使用RS485：0=不使用，1=使用 */
#endif

/* 根据选择的USART定义相应的宏 */
#if (MB_USART_NR == 1)

  /* USART1外设定义 */
  #define MB_USART                        USART1
  #define MB_USART_IRQn                   USART1_IRQn
  #define MB_USART_IRQHandler             USART1_IRQHandler
  #define MB_USART_CLK_ENABLE()           __HAL_RCC_USART1_CLK_ENABLE()
  #define MB_USART_CLK_DISABLE()          __HAL_RCC_USART1_CLK_DISABLE()

  /* USART1引脚定义 */
  #define MB_TX_GPIO_PORT                GPIOA
  #define MB_TX_GPIO_PIN                 GPIO_PIN_9
  #define MB_RX_GPIO_PORT                GPIOA
  #define MB_RX_GPIO_PIN                 GPIO_PIN_10

  /* USART1引脚时钟和复用功能定义 */
  #define MB_TX_GPIO_CLK_ENABLE()         __HAL_RCC_GPIOA_CLK_ENABLE()
  #define MB_TX_GPIO_CLK_DISABLE()        __HAL_RCC_GPIOA_CLK_DISABLE()
  #define MB_TX_AF                        GPIO_AF7_USART1
  #define MB_RX_GPIO_CLK_ENABLE()         __HAL_RCC_GPIOA_CLK_ENABLE()
  #define MB_RX_GPIO_CLK_DISABLE()        __HAL_RCC_GPIOA_CLK_DISABLE()
  #define MB_RX_AF                        GPIO_AF7_USART1

#elif (MB_USART_NR == 2)

  /* USART2外设定义 */
  #define MB_USART                        USART2
  #define MB_USART_IRQn                   USART2_IRQn
  #define MB_USART_IRQHandler             USART2_IRQHandler
  #define MB_USART_CLK_ENABLE()           __HAL_RCC_USART2_CLK_ENABLE()
  #define MB_USART_CLK_DISABLE()          __HAL_RCC_USART2_CLK_DISABLE()

  /* USART2引脚定义 */
  #define MB_TX_GPIO_PORT                GPIOA
  #define MB_TX_GPIO_PIN                 GPIO_PIN_2
  #define MB_RX_GPIO_PORT                GPIOA
  #define MB_RX_GPIO_PIN                 GPIO_PIN_3

  /* USART2引脚时钟和复用功能定义 */
  #define MB_TX_GPIO_CLK_ENABLE()         __HAL_RCC_GPIOA_CLK_ENABLE()
  #define MB_TX_GPIO_CLK_DISABLE()        __HAL_RCC_GPIOA_CLK_DISABLE()
  #define MB_TX_AF                        GPIO_AF7_USART2
  #define MB_RX_GPIO_CLK_ENABLE()         __HAL_RCC_GPIOA_CLK_ENABLE()
  #define MB_RX_GPIO_CLK_DISABLE()        __HAL_RCC_GPIOA_CLK_DISABLE()
  #define MB_RX_AF                        GPIO_AF7_USART2

#else
  #error "Unsupported MB_USART configuration. Please define MB_USART_NR as 1 or 2."
#endif

/* 串口TX和RX引脚初始化宏 */
#define MB_TX_GPIO_Init() do{ \
    MB_TX_GPIO_CLK_ENABLE(); \
    GPIO_InitTypeDef gpio_initure; \
    gpio_initure.Pin = MB_TX_GPIO_PIN; \
    gpio_initure.Mode = GPIO_MODE_AF_PP; \
    gpio_initure.Pull = GPIO_PULLUP; \
    gpio_initure.Speed = GPIO_SPEED_FREQ_HIGH; \
    gpio_initure.Alternate = MB_TX_AF; \
    HAL_GPIO_Init(MB_TX_GPIO_PORT, &gpio_initure); \
}while(0)

#define MB_RX_GPIO_Init() do{ \
    MB_RX_GPIO_CLK_ENABLE(); \
    GPIO_InitTypeDef gpio_initure; \
    gpio_initure.Pin = MB_RX_GPIO_PIN; \
    gpio_initure.Mode = GPIO_MODE_AF_PP; \
    gpio_initure.Pull = GPIO_PULLUP; \
    gpio_initure.Speed = GPIO_SPEED_FREQ_HIGH; \
    gpio_initure.Alternate = MB_RX_AF; \
    HAL_GPIO_Init(MB_RX_GPIO_PORT, &gpio_initure); \
}while(0)

/* RS485引脚定义 - 当MB_USE_RS485=1时生效 */
#if (MB_USE_RS485 == 1)

  /* RE控制引脚定义 - 可在项目中重新定义 */
  #ifndef MB_RS485_RE_GPIO_PORT
    #define MB_RS485_RE_GPIO_PORT          GPIOG
  #endif
  #ifndef MB_RS485_RE_GPIO_PIN
    #define MB_RS485_RE_GPIO_PIN           GPIO_PIN_8
  #endif
  #ifndef MB_RS485_RE_GPIO_CLK_ENABLE
    #define MB_RS485_RE_GPIO_CLK_ENABLE()  do{ __HAL_RCC_GPIOG_CLK_ENABLE(); }while(0)
  #endif

  /* RS485发送/接收控制宏 */
  #define MB_RS485_RE(x)   do{ x ? \
                              HAL_GPIO_WritePin(MB_RS485_RE_GPIO_PORT, MB_RS485_RE_GPIO_PIN, GPIO_PIN_SET) : \
                              HAL_GPIO_WritePin(MB_RS485_RE_GPIO_PORT, MB_RS485_RE_GPIO_PIN, GPIO_PIN_RESET); \
                          }while(0)

  /* RS485初始化辅助宏 */
  #define MB_RS485_RE_GPIO_Init() do{ \
      GPIO_InitTypeDef gpio_initure; \
      gpio_initure.Pin = MB_RS485_RE_GPIO_PIN; \
      gpio_initure.Mode = GPIO_MODE_OUTPUT_PP; \
      gpio_initure.Pull = GPIO_PULLUP; \
      gpio_initure.Speed = GPIO_SPEED_FREQ_HIGH; \
      HAL_GPIO_Init(MB_RS485_RE_GPIO_PORT, &gpio_initure); \
      MB_RS485_RE(0); /* 默认为接收模式 */ \
  }while(0)

#else
  /* 不使用RS485时，将控制宏定义为空操作 */
  #define MB_RS485_RE(x)
  #define MB_RS485_RE_GPIO_Init()
#endif

/* 定时器调试配置 */
//#define MB_TIMER_DEBUG              1           /* 调试模式开关：0=关闭, 1=开启 */
#define MB_TIMER_DEBUG_PORT         GPIOA       /* 调试GPIO端口 */
#define MB_TIMER_DEBUG_PIN          GPIO_PIN_5  /* 调试GPIO引脚（PA5通常连接到板载LED） */

/* 调试辅助函数 */
#if MB_TIMER_DEBUG == 1
static inline void vMBTimerDebugSetHigh( void )
{
    HAL_GPIO_WritePin( MB_TIMER_DEBUG_PORT, MB_TIMER_DEBUG_PIN, GPIO_PIN_SET );
}

static inline void vMBTimerDebugSetLow( void )
{
    HAL_GPIO_WritePin( MB_TIMER_DEBUG_PORT, MB_TIMER_DEBUG_PIN, GPIO_PIN_RESET );
}
#else
#define vMBTimerDebugSetHigh()
#define vMBTimerDebugSetLow()
#endif


/* 函数声明 */
void MB_Uart_Init(void);  /* UART初始化函数 */

#endif /* _PORT_INTERNAL_H */
